package me.ywy.simplelauncher.base;

import me.ywy.ltnetgame.test.apkinfo.R;
import android.appwidget.AppWidgetHostView;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RemoteViews;

public class WidgetHostView extends AppWidgetHostView {
	private CheckLongPressHelper mLongPressHelper;
	private LayoutInflater mInflater;
	private Context mContext;
	private int mPreviousOrientation;

	public WidgetHostView(Context context) {
		super(context);
		mContext = context;
		mLongPressHelper = new CheckLongPressHelper(this);
		mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	protected View getErrorView() {
		return mInflater.inflate(R.layout.widget_error, this, false);
	}

	@Override
	public void updateAppWidget(RemoteViews remoteViews) {
		// Store the orientation in which the widget was inflated
		mPreviousOrientation = mContext.getResources().getConfiguration().orientation;
		super.updateAppWidget(remoteViews);
	}

	public boolean orientationChangedSincedInflation() {
		int orientation = mContext.getResources().getConfiguration().orientation;
		if (mPreviousOrientation != orientation) {
			return true;
		}
		return false;
	}

	public boolean onInterceptTouchEvent(MotionEvent ev) {
		// Consume any touch events for ourselves after longpress is triggered
		if (mLongPressHelper.hasPerformedLongPress()) {
			mLongPressHelper.cancelLongPress();
			return true;
		}
		// Watch for longpress events at this level to make sure
		// users can always pick up this widget
		switch (ev.getAction()) {
		case MotionEvent.ACTION_DOWN: {
			mLongPressHelper.postCheckForLongPress();
			break;
		}
		case MotionEvent.ACTION_MOVE:
		case MotionEvent.ACTION_UP:
		case MotionEvent.ACTION_CANCEL:
			mLongPressHelper.cancelLongPress();
			break;
		}
		// Otherwise continue letting touch events fall through to children
		return false;
	}

	@Override
	public void cancelLongPress() {
		super.cancelLongPress();
		mLongPressHelper.cancelLongPress();
	}

	@Override
	public int getDescendantFocusability() {
		return ViewGroup.FOCUS_BLOCK_DESCENDANTS;
	}
}